@file:JvmName("TipsToast")

package com.wn.framework.toast

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.Toast
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import com.wn.framework.R
import com.wn.framework.databinding.WidgetTipsToastBinding
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton

/**
 * @Author: wn
 * @Date: 2024/7/8 17:16
 * @Description: 提示 弹框 封装
 * Object 无法 hilt 这里改为 class 单利的形式
 * hilt 直接提供 instance  需要用的地方 直接 inject -- instance -- .fun
 */
@Singleton
class TipsToast {
  @Inject
  @ApplicationContext
  lateinit var mContext: Context

  /**
   * 单例模式
   */
  companion object {
    @Volatile
    private var INSTANCE: TipsToast? = null

    @JvmStatic
    @Synchronized
    fun getInstance() = INSTANCE ?: TipsToast().also { INSTANCE = it }
  }

  private var toast: Toast? = null

  private val mToastHandler by lazy {
    Looper.myLooper()?.run { Handler(this) }
  }

  private val mBinding by lazy {
    WidgetTipsToastBinding.inflate(LayoutInflater.from(mContext), null, false)
  }

/*
  fun init(context: Application) {
    mContext = context
  }
*/

  /**
   * 展示 提示
   * @param msg String -- 内容
   * @param duration Int -- 时间间隔
   * @param drawableId Int -- 是否带图片
   */
  private fun showTipsImpl(msg: String, duration: Int, @DrawableRes drawableId: Int = 0) {
    if (msg.isEmpty()) return
    // 取消上一次的 提示
    toast?.apply {
      this@TipsToast.cancel()
    }
    // 开始提示
    mToastHandler?.postDelayed({
      runCatching {
        mBinding.tipToastTxt.apply {
          text = msg
          // 位于左边
          setCompoundDrawablesWithIntrinsicBounds(drawableId, 0, 0, 0)
        }
        toast = Toast(mContext)
        toast?.apply {
          view = mBinding.root
          setGravity(Gravity.CENTER, 0, 0)
          this.duration = duration
          show()
        }
      }.onFailure {
        Log.e("show tips error", "$it")
      }
    }, 50)
  }

  // 取消 toast
  private fun cancel() {
    toast?.cancel()
    toast = null
    mToastHandler?.removeCallbacksAndMessages(null)
  }

  /**
   * string 引用
   * @param stringId Int
   */
  fun showTips(@StringRes stringId: Int) {
    val msg = mContext.getString(stringId)
    showTipsImpl(msg, Toast.LENGTH_SHORT)
  }

  /**
   * string
   * @param msg String?
   */
  fun showTips(msg: String) {
    showTipsImpl(msg, Toast.LENGTH_SHORT)
  }

  // 时间间隔
  fun showTips(msg: String, duration: Int) {
    showTipsImpl(msg, duration)
  }

  fun showTips(@StringRes stringId: Int, duration: Int) {
    val msg = mContext.getString(stringId)
    showTipsImpl(msg, duration)
  }

  // 成功图标
  fun showSuccessTips(msg: String) {
    showTipsImpl(msg, Toast.LENGTH_SHORT, R.mipmap.widget_toast_success)
  }

  fun showSuccessTips(@StringRes stringId: Int) {
    val msg = mContext.getString(stringId)
    showTipsImpl(msg, Toast.LENGTH_SHORT, R.mipmap.widget_toast_success)
  }

  // 失败图标
  fun showWarningTips(msg: String) {
    showTipsImpl(msg, Toast.LENGTH_SHORT, R.mipmap.widget_toast_warning)
  }

  fun showWarningTips(@StringRes stringId: Int) {
    val msg = mContext.getString(stringId)
    showTipsImpl(msg, Toast.LENGTH_SHORT, R.mipmap.widget_toast_warning)
  }
}